package com.example.baiduai.service;

import com.baidu.aip.nlp.AipNlp;
import com.baidu.aip.nlp.ESimnetType;
import com.example.baiduai.factory.AipFactory;
import org.json.JSONObject;

import java.io.PrintWriter;
import java.util.HashMap;

/**
 * @author wangyizhu
 * @version "1.8.0_131"
 * @email 2641956765@qq.com
 * @desc:
 * @Time 2022/09/23 14:40
 */
public class NlpService {

    /**
     * // 情感倾向分析
     *
     * @param content
     * @return
     */
    public String emotion(String content) {
        HashMap<String, Object> options = new HashMap<String, Object>();
        AipNlp client = AipFactory.getNLPInstance();
        JSONObject res = client.sentimentClassify(content, options);
        return res.toString();
    }

    /**
     * // 地址识别接口
     *
     * @param content
     * @return
     */
    public String address(String content) {
        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        AipNlp client = AipFactory.getNLPInstance();
        JSONObject res = client.address(content, options);
        return res.toString(2);
    }

    /**
     * // 词法分析
     *
     * @param content
     * @return
     */
    public String lexical(String content) {
        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        AipNlp client = AipFactory.getNLPInstance();

        JSONObject res = client.lexer(content, options);
        return res.toString(2);
    }

    /**
     * // 新闻摘要接口
     *
     * @param content
     * @return
     */
    public String headline(String content) {
        int maxSummaryLen = 300;
        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        AipNlp client = AipFactory.getNLPInstance();
        options.put("title", "标题");

        JSONObject res = client.newsSummary(content, maxSummaryLen, options);
        return res.toString(2);
    }

    /**
     * // 对话情绪识别接口
     *
     * @param content
     * @return
     */
    public String Dialogue(String content) {
        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        AipNlp client = AipFactory.getNLPInstance();
        options.put("scene", "talk");

        JSONObject res = client.emotion(content, options);
        return res.toString(2);
    }

    /**
     * // DNN语言模型
     *
     * @param content
     * @return
     */
    public String dnnlmCn(String content) {
        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        AipNlp client = AipFactory.getNLPInstance();

        JSONObject res = client.dnnlmCn(content, options);
        return (res.toString(2));
    }

    /**
     * // 文章分类
     *
     * @param title
     * @param content
     * @return
     */
    public String category(String title, String content) {
        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        AipNlp client = AipFactory.getNLPInstance();

        JSONObject res = client.topic(title, content, options);
        return res.toString(2);
    }

    /**
     * // 获取美食评论情感属性
     *
     * @param content
     * @return
     */
    public String commentTag(String content) {
        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        AipNlp client = AipFactory.getNLPInstance();
        JSONObject response = client.commentTag(content, ESimnetType.FOOD, options);
        if (response == null) {
            JSONObject response2 = client.commentTag(content, ESimnetType.HOTEL, options);
            return (response2.toString());
        }
        return (response.toString());
    }

    /**
     * // 依存句法分析
     *
     * @param content
     * @return
     */
    public String depParser(String content) {
        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        options.put("mode", 1);
        AipNlp client = AipFactory.getNLPInstance();

        JSONObject res = client.depParser(content, options);
        return (res.toString(2));
    }

    /**
     * // 词向量表示
     *
     * @param content
     * @return
     */
    public String wordEmbedding(String content) {
        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        AipNlp client = AipFactory.getNLPInstance();

        JSONObject res = client.wordEmbedding(content, options);
        return (res.toString(2));
    }

    /**
     * // 词义相似度
     *
     * @param word1
     * @param word2
     * @return
     */
    public String wordSimEmbedding(String word1, String word2) {
        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        AipNlp client = AipFactory.getNLPInstance();


        JSONObject res = client.wordSimEmbedding(word1, word2, options);
        return (res.toString(2));
    }

    /**
     * // 短文本相似度
     *
     * @param text1
     * @param text2
     * @return
     */
    public String simnet(String text1, String text2) {
        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        AipNlp client = AipFactory.getNLPInstance();
        options.put("model", "CNN");


        JSONObject res = client.simnet(text1, text2, options);
        return (res.toString(2));
    }

    /**
     * // 文章标签
     *
     * @param title
     * @param content
     * @return
     */
    public String keyword(String title, String content) {

        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        AipNlp client = AipFactory.getNLPInstance();

        JSONObject res = client.keyword(title, content, options);
        return (res.toString(2));
    }

    /**
     * // 文本纠错
     *
     * @param content
     * @return
     */
    public String ecnet(String content) {
        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        AipNlp client = AipFactory.getNLPInstance();

        JSONObject res = client.ecnet(content, options);
        return (res.toString(2));
    }

    /**
     * // 词法分析（定制版）
     *
     * @param content
     * @return
     */
    public String lexerCustom(String content) {
        // 传入可选参数调用接口
        HashMap<String, Object> options = new HashMap<String, Object>();
        AipNlp client = AipFactory.getNLPInstance();

        JSONObject res = client.lexerCustom(content, options);
        return (res.toString(2));
    }


}
